import { getToken, setToken, removeToken } from '@/utils/auth'
import { login as loginapi, getUserInfo } from '@/api/user'
const state = {
  token: getToken(),
  userinfo: {}
}
const mutations = {
  updataToken(state, token) {
    state.token = token
    setToken(token)
  },
  delToken(state) {
    state.token = null
    removeToken()
  },
  updataUserInfo(state, data) {
    state.userinfo = data
  }

}
const actions = {
  async login(context, data) {
    const res = await loginapi(data)
    // console.log(res)
    // console.log('掉用了接口', data)
    context.commit('updataToken', res)
  },
  async getUserInfo(context) {
    const res = await getUserInfo()
    // console.log(res)
    context.commit('updataUserInfo', res)
  },
  logout({ commit }) {
    commit('delToken')
    commit('updataUserInfo', {})
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
